package com.busi.ecTemplate.controller;

import java.io.File;
import java.util.List;

import com.busi.ecTemplate.entity.Template;
import com.busi.ecTemplate.service.TemplateService;
import com.busi.ecTemplateItem.entity.TemplateItem;
import com.busi.utils.DictUtil;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.zcurd.common.DBTool;
import com.zcurd.common.ZcurdTool;
import com.zcurd.common.base.BaseController;
import com.zcurd.common.util.StringUtil;

public class TemplateController extends BaseController {

	// 列表界面
	public void listPage() {
		setAttr("classify", DictUtil.dict.getDictList("ec_classify"));
		render("list.html");
	}

	// 获取列表数据
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		String orderBy = getOrderBy();
		if (StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		List<Record> list = DBTool.findByMultPropertiesDbSource("zcurd_busi", "t_b_template", properties, symbols,
				values, orderBy, getPager());
		ZcurdTool.replaceDict(DictUtil.dict.getDictList("ec_classify"), list, "classify_value");
		renderDatagrid(list,
				DBTool.countByMultPropertiesDbSource("zcurd_busi", "t_b_template", properties, symbols, values));
	}

	// 添加界面
	public void addPage() {
		setAttr("classify", DictUtil.dict.getDictList("ec_classify"));
		render("add.html");
	}

	// 添加功能实现
	public void add() {
		getModel(Template.class, "model").save();
		renderSuccess();
	}

	// 修改页面
	public void updatePage() {
		setAttr("classify", DictUtil.dict.getDictList("ec_classify"));
		setAttr("model", Template.dao.findById(getPara("id")));
		render("update.html");
	}

	// 修改
	public void update() {
		Template model = Template.dao.findById(getPara("id"));
		model.set("template_name", getPara("model.template_name"));
		model.set("classify_value", getPara("model.classify_value"));
		model.update();
		renderSuccess();
	}

	// 删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new Template().set("id", id).delete();

		}
		renderSuccess();
	}

	// 删除上传的模板文件及数据
	public void deleteFile() {
		long id = getParaToLong("id");
		TemplateItem model = TemplateItem.dao.findById(id);
		String filePath = model.getStr("template_item_path");
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
		model.set("id", id).delete();
		renderSuccess();
	}

	// 文件上传界面
	public void uploadPage() {
		long id = getParaToLong("id");
		setAttr("id", id);
		render("upload.html");
	}

	// 文件上传实现
	public void upload() {
		try {
			UploadFile file = getFile();
			long templateId = getParaToLong("templateId");
			TemplateItem model = getModel(TemplateItem.class, "model");
			TemplateService templateService = Duang.duang(TemplateService.class);
			templateService.uploadFile(file, model, templateId);
			renderSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			renderError(-1);
		}
	}

	// 模板下载界面
	public void downloadPage() {
		long id = getParaToLong("id");
		setAttr("id", id);
		render("download.html");
	}

	// 获取列表数据
	public void templateItemData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		String orderBy = getOrderBy();
		if (StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		List<Record> list = DBTool.findByMultPropertiesDbSource("zcurd_busi", "t_b_template_item", properties, symbols,
				values, orderBy, getPager());
		renderDatagrid(list,
				DBTool.countByMultPropertiesDbSource("zcurd_busi", "t_b_template_item", properties, symbols, values));
	}

	// 模板下载
	public void templateDownload() {
		long id = getParaToLong("id");
		TemplateItem item = TemplateItem.dao.findById(id);
		String filePath = item.getTemplateItemPath();
		File file = new File(filePath);
		renderFile(file, item.getTemplateItemName());
	}
}
